package com.iss.umservice.controller;

import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.iss.iframework.security.OperatorUserEntry;
import com.iss.iframework.security.OperatorUserInfomation;
import com.iss.iframework.util.StringUtil;
import com.iss.iframework.web.ResultCall;
import com.iss.iframework.web.support.tld.pagination.Navigation;
import com.iss.iframework.web.support.tld.pagination.PageUtil;
import com.iss.iframework.web.support.tld.pagination.Pagination;
import com.iss.umservice.bean.ResourceBean;
import com.iss.umservice.bean.RoleBean;
import com.iss.umservice.bean.SystemBean;
import com.iss.umservice.manager.ResourceManager;
import com.iss.umservice.manager.RoleManager;
import com.iss.umservice.manager.SystemManager;
import com.iss.umservice.soa.impl.ChangePasswordResponse;

/**
 * 角色信息管理控制器
 * 
 * @author suxiaoping
 * @date 2012-8-13
 */
@Controller
@RequestMapping("/role/*")
@SessionAttributes("curUser")
public class RoleController
{
    @Autowired
    private RoleManager roleManager;
    
    @Autowired
    private ResourceManager resourceManager;
    
    @Autowired
    private SystemManager systemManager;
    
    /**
     * 查询的角色
     * 
     * @param roleName //角色名称
     * @return  void
     */
    @RequestMapping
    public void queryRoleInfos(@ModelAttribute("curUser") OperatorUserEntry userEntry,String roleName, String systemId, String pages,
            Map<String, Object> model)
    {
        //统计角色个数
        int count = roleManager.countRoleInfo(roleName, systemId);
        //分页
        Pagination pagination = PageUtil.createPage(pages,
                count,
                "../role/queryRoleInfos.html");
        
        //查询角色记录
        List<RoleBean> beanList = roleManager.queryRoleInfos(roleName,
                systemId,
                
                pagination.getOffset(),
                pagination.getRowSize());
        List<SystemBean> systemBeanList = systemManager.querySystemList();
        
        //条件放入分页当中
        pagination.addCondition("roleName", roleName);
        pagination.addCondition("systemId", systemId);
        
        //存入MAP
        model.put("systemBeanList", systemBeanList);
        model.put("roleBean", beanList);
        model.put("systemId", systemId);
        
        model.put(Navigation.PAGINATION, pagination);
        
    }
    
    /**
     * 跳转界面
     * @return
     */
    @RequestMapping
    public void insertRoleInfo()
    {
        
    }
    
    /**
     * 增加角色信息
     * 
     * @param bean 角色实体类
     * @param model map
     * @return
     */
    @RequestMapping
    public void addRoleInfo(RoleBean roleBean)
    {
        //生成角色ID
        String roleId = StringUtil.getPrimarySessionId();
        
        //设置ID
        roleBean.setRoleId(roleId);
        
        //添加岗位信息
        roleManager.addRoleInfo(roleBean);
        
        ResultCall.makeSuccess("../role/queryRoleInfos.html");
        
    }
    
    /**
     * 修改跳转界面
     * @return
     */
    @RequestMapping
    public String updateRoleInfo(String roleId, Map<String, Object> model)
    {
        //获取角色信息
        RoleBean roleInfo = roleManager.queryRoleInfoByRoleId(roleId);
        
        //获取岗位与角色对应关系
        List<ResourceBean> beanList = roleManager.queryResourceInfoByRoleId(roleId);
        
        //角色名称
        String resourceName = "";
        
        //角色ID
        String resourceId = "";
        
        //合并角色ID和角色名称
        if (null != beanList && 0 != beanList.size())
        {
            for (int i = 0; i < beanList.size(); i++)
            {
                ResourceBean resourceBean = (ResourceBean)beanList.get(i);
                
                resourceName += resourceBean.getResourceName() + ",";
                
                resourceId += resourceBean.getResourceId() + ",";
            }
        }
        
        //去掉最后一个逗号
        if (!"".equals(resourceName) && 0 != resourceName.length())
        {
            resourceName = resourceName.substring(0, resourceName.length() - 1);
        }
        
        //去掉最后一个逗号
        if (!"".equals(resourceId) && 0 != resourceId.length())
        {
            resourceId = resourceId.substring(0, resourceId.length() - 1);
        }
        
        roleInfo.setResourceId(resourceId);
        
        model.put("roleBean", roleInfo);
        model.put("resourceName", resourceName);
        
        return "/role/modifyRoleInfo";
    }
    
    /**
     * 修改角色信息与资源对应信息
     * 
     * @param bean 岗位实体类
     * @param model map
     * @return
     */
    @RequestMapping
    public void modifyRoleInfo(RoleBean roleBean)
    {
        //修改岗位信息
        roleManager.updateRoleInfo(roleBean);
        
        //跳转的界面
        ResultCall.makeSuccess("../role/queryRoleInfos.html");
        
    }
    
    /**
     * 删除角色信息及角色与资源对应信息
     * 
     * @param roleId 角色ID
     * @param model map
     * @return
     */
    @RequestMapping
    public void delRoleInfo(String roleId)
    {
        //修改岗位信息
        roleManager.delRoleInfo(roleId);
        
        //跳转的界面
        ResultCall.makeSuccess("../role/queryRoleInfos.html");
        
    }
    
    /**
     * 根据系统ID查询该系统的资源
     * 
     * @param systemId 系统ID
     * @return  void
     */
    @RequestMapping
    public void queryResourceBySystemId(ResourceBean resourceBean,
            String pages, Map<String, Object> model)
    {
        //统计
        int count = resourceManager.countResourceInfo(resourceBean);
        //分页
        Pagination pagination = PageUtil.createPage(pages,
                count,
                "../role/queryResourceBySystemId.html");
        
        //修改岗位信息
        List<ResourceBean> beanList = resourceManager.queryResourceInfos(resourceBean,
                pagination.getOffset(),
                pagination.getRowSize());
        
        //条件放入分页当中
        pagination.addCondition("resourceName", resourceBean.getResourceName());
        pagination.addCondition("systemId", resourceBean.getSystemId());
        
        //存入MAP
        model.put("resourceBean", beanList);
        model.put("systemId", resourceBean.getSystemId());
        
        model.put(Navigation.PAGINATION, pagination);
        
    }
    
    /**
     * 根据角色ID查询该角色的资源
     * 
     * @param roleId 角色ID
     * @return  void
     */
    @RequestMapping
    public void queryResourceInfo(String roleId, Map<String, Object> model)
    {
        
        //查询资源信息
        List<ResourceBean> beanList = roleManager.queryResourceInfoByRoleId(roleId);
        
        //查询角色信息
        RoleBean roleBean = roleManager.queryRoleInfoByRoleId(roleId);
        
        //存入MAP
        model.put("resourceBean", beanList);
        
        model.put("roleBean", roleBean);
        
    }
    
    /**
     * 校验角色名称是否已存在
     * @param stationName
     * @param response
     * @throws UnsupportedEncodingException
     */
    @RequestMapping
    public void checkRoleName(String roleName, HttpServletResponse response) throws UnsupportedEncodingException
    {
        roleName = URLDecoder.decode(roleName,"UTF-8");
       String isExist = roleManager.checkRoleName(roleName);
       PrintWriter writer = null;
       try
       {
           response.setCharacterEncoding("UTF-8");
           writer = response.getWriter();
           writer.write(isExist);
       }
       catch (Exception e) {
    }
       
    }
}
